<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Customers;
use Hash;
use App\Cat;
use App\Http\Controllers\Controller;

class RegController extends Controller
{
    //注册界面展示
   public function getReg(){
       $cat = new Cat;
       $cats = $cat->getTree();
       return view('home.register',['cats'=>$cats]);
   }

    //注册完成设置session
   public function postReg(Request $request){

       $this->validate($request,[
               'name'=>'required|unique:customers,name|min:3|max:20',
               'mobile'=>'required|regex:/^1[34578]\d{9}$/',
               'email'=>'required|unique:customers,email|email',
               'password'=>'required|min:6|max:20'
           ],[
               'name.required' => '必须填写用户名',
               'mobile.required' => '必须填写手机号',
               'email.required' => '必须填写邮箱地址',
               'password.required' => '必须填写密码',
               'name.unique'=>'用户名已存在',
               'email.unique'=>'邮箱已经注册过了',
               'email'=>'请提交正确的邮箱地址',
               'name.min' => '用户名必须大于三',
               'password.min' => '密码必须大于六位',
               'name.max' => '用户名不能大于20',
               'password.max' => '密码不能大于20',
               'regex' => '必须填写真实手机号'
           ]);
       $cus = new Customers;
       $cus->name = $request->name;
       $cus->email = $request->email;
       $cus->mobile = $request->mobile;
       $cus->regtime = time();
       $cus->remember_token = md5($cus->email.$cus->name);
//       $cus->password = md5($request->password.$cus->regtime);
       $cus->password = hash::make($request->password);
       $cus->save();
       $request->session()->put('remt',$cus->remember_token);
       $request->session()->put('name',$cus->name);
	   $request->session()->put('uid', $cus->uid);
       return redirect('/home');
   }
}
